<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计字符串中每个字符的个数？以对象的形式返回</title>
</head>
<body>
    
</body>
<script>
    let str = 'abcdacbabcbababcbabcabd';

    let strToArr = str.split('');

    let obj = strToArr.reduce(function (pre, cur) {
        if (pre[cur]) {
            pre[cur] += 1; 
        } else {
            pre[cur] = 1;
        }
        return pre;
    }, {})

    for (let key in obj) {
        console.log(key + '出现次数为' + obj[key]);
    }
</script>
</html>